/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.deprecated.jiangxianli;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.core.tester.ProxyTypeEnum;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;
import org.shoulder.core.converter.LocalDateTimeConverter;

import java.time.Duration;
import java.util.List;

/**
 * https://ip.jiangxianli.com/?page=1  通常只有1页，加载快，可以根据参数查询
 * page: "1",
 * protocol: "http",
 * anonymity: "",
 * country: "",
 * isp: "", 运营商
 *
 * @author lym
 * @deprecated 2023.4 变成个人博客网站了
 */
//@Component
public class JiangxianliResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public JiangxianliResultHandler() {
        super("jiangxianli");
    }

    protected Duration defaultLimitRateDuration() {
        return Duration.ofSeconds(5);
    }

    @Override
    public void startSpiders() {
        IpProxySpiderTask task = createTask("https://ip.jiangxianli.com/?page=1");
        SpiderTaskQueue.putTask(task);
    }

    /**
     * 每 10 分钟触发一次
     */
    @Override
    public String executeCron() {
        return "0 0/10 * * * ?";
    }

    /**
     * 处理响应
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        String regex = "<tr><td>(.*?)</td>" +
                "<td>(.*?)</td>" +
                "<td>(.*?)</td>" +
                "<td>(.*?)</td>" +
                "<td>(.*?)</td>" +
                // 所属地区
                "<td>.*?</td>" +
                "<td>(.*?)</td>" +
                "<td>(.*?)秒</td>" +
                // 存活时长
                "<td>.*?</td>" +
                "<td>(.*?)</td><td>";

        List<String> ipList = ReUtil.findAll(regex, html, 1);
        List<String> portList = ReUtil.findAll(regex, html, 2);
        // 透明 / 高匿
        List<String> anonymityList = ReUtil.findAll(regex, html, 3);
        List<String> typeList = ReUtil.findAll(regex, html, 4);
        List<String> locationList = ReUtil.findAll(regex, html, 5);
        List<String> operatorList = ReUtil.findAll(regex, html, 6);
        List<String> responseTimeList = ReUtil.findAll(regex, html, 7);
        List<String> lastVerificationTimeList = ReUtil.findAll(regex, html, 8);

        for (int i = 0; i < ipList.size(); i++) {
            String responseTime = responseTimeList.get(i);
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
                    .anonymity(anonymityList.get(i))
                    .proxyTypeEnum(ProxyTypeEnum.of(typeList.get(i)))
                    .type(ProxyTypeEnum.of(typeList.get(i)).getType())
                    .location(locationList.get(i))
                    .operator(operatorList.get(i))
                    .responseTime(responseTime.contains("毫") ?
                            Long.parseLong(responseTime.replace("毫", ""))
                            : ProxyInfo.parseSecondToMill(responseTime)
                    )
                    .lastVerificationTime(LocalDateTimeConverter.INSTANCE.convert(lastVerificationTimeList.get(i)))
                    .build();

            addProxyToSourceRepository(proxyInfo);
        }

        // 下一页
        int page = Integer.parseInt(ReUtil.findAllGroup1("page: \"(\\d+)\",", html).get(0));
        int total = Integer.parseInt(ReUtil.findAllGroup1("count: \"(\\d+)\",", html).get(0));
        int pageSize = Integer.parseInt(ReUtil.findAllGroup1("limit: \"(\\d+)\",", html).get(0));
        int totalPage = total / pageSize + 1;
        if (page < totalPage) {
            int nextPage = page + 1;
            IpProxySpiderTask nextPageTask = createTask("https://ip.jiangxianli.com/?page=" + nextPage);
            SpiderTaskQueue.putTask(nextPageTask);
        }

    }

}